//
//  BluetoothDisplayView.m
//  Saas
//
//  Created by 杨荣 on 2020/6/1.
//  Copyright © 2020 bing. All rights reserved.
//

#import "BluetoothDisplayView.h"

static const CGFloat kBluetoothDisplayViewWidth = 250;// 宽度
static const CGFloat kBluetoothDisplayViewHeight = 300;// 高度

@interface BluetoothDisplayView ()<UITableViewDataSource,UITableViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic,strong) UIView *mainView;// 主视图
@property (nonatomic,strong) UITableView *tableView;// 列表

@property (nonatomic,strong) NSArray <CBPeripheral *>*peripheralArray;// 设备列表
@end

@implementation BluetoothDisplayView

+ (instancetype)show:(UIView *)superView {
    
    UIView *rootView = [UIApplication sharedApplication].keyWindow;
    if (superView) {
        rootView = superView;
    }
    
    BluetoothDisplayView *view = [[BluetoothDisplayView alloc] init];
    view.frame = CGRectMake(0, 0, rootView.frame.size.width, rootView.frame.size.height);
    view.backgroundColor = [UIColor clearColor];
    [rootView addSubview:view];
    
    UIView *mainView = [[UIView alloc] init];
    mainView.frame = CGRectZero;
    mainView.center = view.center;
    mainView.backgroundColor = [UIColor whiteColor];
    mainView.clipsToBounds = YES;
    mainView.layer.cornerRadius = 5;
    view.mainView = mainView;
    [view addSubview:mainView];
    
    [view show];
    
    return view;
}

- (void)show {
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        self.mainView.width = kBluetoothDisplayViewWidth;
        self.mainView.height = kBluetoothDisplayViewHeight;
        self.mainView.center = self.center;
    } completion:^(BOOL finished) {
        [self setUI];
    }];
}
- (void)hide {
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundColor = [UIColor clearColor];
        self.mainView.width = 0;
        self.mainView.height = 0;
        self.mainView.center = self.center;
    } completion:^(BOOL finished) {
        if (self.hideBlock) {
            self.hideBlock();
        }
        [self.mainView removeFromSuperview];
        [self removeFromSuperview];
    }];
}


- (void)setUI {
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    self.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(mainViewTap)];
    tap2.delegate = self;
    [self.mainView addGestureRecognizer:tap2];
    self.mainView.userInteractionEnabled = YES;
    
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.backgroundColor = [UIColor grayColor];
    titleLabel.text = @"选择蓝牙设备";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.frame = CGRectMake(0, 0, self.mainView.width, 44);
    [self.mainView addSubview:titleLabel];
    
    CGFloat bottom = titleLabel.frame.origin.y + titleLabel.frame.size.height;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:(CGRectMake(0, bottom, self.mainView.width, self.mainView.height - bottom)) style:(UITableViewStylePlain)];
    if (@available(iOS 15.0, *)) {
        tableView.sectionHeaderTopPadding = 0;
    }
    self.tableView = tableView;
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];// 去掉多余的线
    // 使分割线对齐
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins: UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.mainView addSubview:tableView];
    
}
- (void)mainViewTap {
    
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    NSString *touchClass = NSStringFromClass([touch.view class]);
    if ([touchClass isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }else{
        return YES;
    }
}


#pragma mark UITableViewDataSource,UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.peripheralArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellIdentifier = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellIdentifier];
    }
    CBPeripheral *peripheral = self.peripheralArray[indexPath.row];
    cell.textLabel.text = peripheral.name;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (self.connectPeripheralBlock) {
        CBPeripheral *peripheral = self.peripheralArray[indexPath.row];
        self.connectPeripheralBlock(peripheral);
        [self hide];
    }
}

- (void)update:(NSArray <CBPeripheral *>*)peripheralArray {
    self.peripheralArray = peripheralArray;
    [self.tableView reloadData];
}
@end
